package com.yumi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yumi.common.Result;
import com.yumi.entity.TrainingPlan;
import com.yumi.service.TrainingPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 培训计划控制器
 */
@RestController
@Slf4j
@RequestMapping("/api/training/plans")
@RequiredArgsConstructor
@Tag(name = "培训计划管理", description = "培训计划相关接口")
public class TrainingPlanController {

    private final TrainingPlanService trainingPlanService;

    /**
     * 创建培训计划
     * @param trainingPlan 培训计划信息
     * @return 创建结果
     */
    @PostMapping
    @Operation(summary = "创建培训计划", description = "创建一个新的培训计划")
    public Result<TrainingPlan> createTrainingPlan(@Valid @RequestBody TrainingPlan trainingPlan) {
        log.info("创建培训计划：{}", trainingPlan);
        TrainingPlan createdPlan = trainingPlanService.createTrainingPlan(trainingPlan);
        return Result.success(createdPlan);
    }

    /**
     * 分页查询培训计划
     * @param page 页码
     * @param pageSize 每页大小
     * @param title 标题关键字
     * @param type 培训类型
     * @param status 培训状态
     * @return 分页结果
     */
    @GetMapping
    @Operation(summary = "分页查询培训计划", description = "根据条件分页查询培训计划")
    public Result<Page<TrainingPlan>> pageQuery(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer status) {
        
        log.info("分页查询培训计划：page={}, pageSize={}, title={}, type={}, status={}", 
                page, pageSize, title, type, status);
        
        Page<TrainingPlan> pageResult = trainingPlanService.pageQuery(page, pageSize, title, type, status);
        
        return Result.success(pageResult);
    }

    /**
     * 查询培训计划详情
     * @param id 培训计划ID
     * @return 培训计划详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询培训计划详情", description = "根据ID查询培训计划详细信息")
    public Result<TrainingPlan> getTrainingPlanById(@PathVariable Long id) {
        log.info("查询培训计划详情：id={}", id);
        TrainingPlan trainingPlan = trainingPlanService.getTrainingPlanById(id);
        if (trainingPlan != null) {
            return Result.success(trainingPlan);
        }
        return Result.error("培训计划不存在");
    }

    /**
     * 修改培训计划
     * @param id 培训计划ID
     * @param trainingPlan 培训计划信息
     * @return 修改结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "修改培训计划", description = "根据ID修改培训计划信息")
    public Result<TrainingPlan> updateTrainingPlan(
            @PathVariable Long id,
            @Valid @RequestBody TrainingPlan trainingPlan) {
        
        log.info("修改培训计划：id={}, trainingPlan={}", id, trainingPlan);
        
        TrainingPlan updatedPlan = trainingPlanService.updateTrainingPlan(id, trainingPlan);
        
        return Result.success(updatedPlan);
    }

    /**
     * 删除培训计划
     * @param id 培训计划ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除培训计划", description = "根据ID删除培训计划")
    public Result deleteTrainingPlan(@PathVariable Long id) {
        log.info("删除培训计划：id={}", id);
        trainingPlanService.deleteTrainingPlan(id);
        return Result.success();
    }

    /**
     * 更新培训计划状态
     * @param id 培训计划ID
     * @param status 培训状态
     * @return 更新结果
     */
    @PutMapping("/{id}/status/{status}")
    @Operation(summary = "更新培训计划状态", description = "根据ID更新培训计划状态")
    public Result<TrainingPlan> updateTrainingPlanStatus(
            @PathVariable Long id,
            @PathVariable Integer status) {
        
        log.info("更新培训计划状态：id={}, status={}", id, status);
        
        TrainingPlan updatedPlan = trainingPlanService.updateTrainingPlanStatus(id, status);
        
        return Result.success(updatedPlan);
    }
} 